import { env } from 'node:process';
import { NodeSDK } from '@opentelemetry/sdk-node';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-grpc';
import { OTLPMetricExporter } from '@opentelemetry/exporter-metrics-otlp-grpc';
import { OTLPLogExporter } from '@opentelemetry/exporter-logs-otlp-grpc';
import { SimpleLogRecordProcessor } from '@opentelemetry/sdk-logs';
import { PeriodicExportingMetricReader } from '@opentelemetry/sdk-metrics';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
import { credentials } from '@grpc/grpc-js';

const environment = process.env.NODE_ENV || 'development';
const otlpServer = env.OTEL_EXPORTER_OTLP_ENDPOINT;

if (otlpServer) {
  console.log(`OTLP endpoint: ${otlpServer}`);

  const isHttps = otlpServer.startsWith('https://');
  const collectorOptions = {
    credentials: !isHttps ? credentials.createInsecure() : credentials.createSsl()
  };

  const sdk = new NodeSDK({
    traceExporter: new OTLPTraceExporter(collectorOptions),
    metricReader: new PeriodicExportingMetricReader({
      exportIntervalMillis: environment === 'development' ? 5000 : 10000,
      exporter: new OTLPMetricExporter(collectorOptions),
    }),
    logRecordProcessor: new SimpleLogRecordProcessor({
      exporter: new OTLPLogExporter(collectorOptions)
    }),
    instrumentations: [
      new HttpInstrumentation(),
      new ExpressInstrumentation()
  ],
  });

  sdk.start();
}
